package links

import (
	"fmt"
	"gluenet/pkg/apis"
	"gluenet/pkg/meta"
)

func GetStrategyLinks(ctx *apis.GlueContext) {
	var (
		err error
		key string
	)
	key = ctx.WebContext.URLParam("guid")

	ctx.Logger.Debugf("get strategy guid: %v", key)

	//查询数据库
	var ags *meta.StrategyLinks
	if err = ctx.DataBase.StrategyLinksGet(key, &ags); err != nil {
		ctx.Logger.Errorf("query error %v", err)
		ctx.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("query error:%v", err), nil))
		return
	}

	//if len(ags) == 0 {
	//	ctx.Logger.Warnf("data is nil")
	//	ctx.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("data is nil"), nil))
	//	return
	//}

	//返回结果
	n, _ := ctx.WebContext.JSON(apis.ResponseOK(meta.DeCodeLinks(ags.ApplicationGuids)))
	ctx.Logger.Debugf("list config response bytes: %v", n)
}

func GetStrategyUnLinks(ctx *apis.GlueContext) {
	var (
		err error
		key string
	)
	key = ctx.WebContext.URLParam("guid")

	ctx.Logger.Debugf("get strategy guid: %v", key)

	//查询数据库
	var guids []string
	if err = ctx.DataBase.StrategyUnLinksGet(key, &guids); err != nil {
		ctx.Logger.Errorf("query error %v", err)
		ctx.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("query error:%v", err), nil))
		return
	}

	//返回结果
	n, _ := ctx.WebContext.JSON(apis.ResponseOK(guids))
	ctx.Logger.Debugf("list config response bytes: %v", n)
}
